using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace TaugeLibrary.UserControls
{
    public class NumericTextBox:ExtendedTextBox 
    {
        protected decimal m_value;
        protected string m_format = "#,##0.00;(#,##0.00);0.00";
        protected string m_format_edit = "###0.00";

        public NumericTextBox()
        {
            SuspendLayout();
            ResumeLayout(false);
            TextAlign=HorizontalAlignment.Right;
        }

        public decimal Value{
            set { 
                m_value = value;
                if (Focused)
                    Text = m_value.ToString(m_format_edit);
                else
                    Text = m_value.ToString(m_format);
                }
            get {   Decimal.TryParse( Text, System.Globalization.NumberStyles.Number, null, out m_value);
                    //Decimal.TryParse(Text,m_value);
                    return m_value;
                }
        }

        public string Format
        {
            set { m_format = value; }
            get { return m_format; }
        }

        public string FormatEdit
        {
            set { m_format_edit = value; }
            get { return m_format_edit; }
        }

        //protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        //{
        //    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
        //        e.Handled = true;
        //    base.OnKeyPress(e);
        //}


        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //}

        protected override void OnGotFocus(EventArgs e)
        {
            Text = m_value.ToString(m_format_edit);            
            base.OnGotFocus(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if(Enabled && !ReadOnly)
            {
                try{
                    Decimal.TryParse(Text,System.Globalization.NumberStyles.Number,null,out m_value);
                }catch{
                }
                int KeyAscii;
                KeyAscii=(int)e.KeyChar;
                if (char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar))
                {
                }
                else if (KeyAscii >= 44 && KeyAscii <= 46)
                {
                    if (Text.IndexOf(e.KeyChar) >= 0)
                        KeyAscii = 0;

                    if (KeyAscii == 45 && SelectionStart != 0)
                        KeyAscii = 0;
                }
                else
                    KeyAscii = 0;

                e.Handled = (KeyAscii == 0);
                base.OnKeyPress(e);
            }
        }

        protected override void OnLostFocus(EventArgs e)
        {
            if (Text.Trim() == "")
                Text = "0";
            Decimal.TryParse(Text, System.Globalization.NumberStyles.Number, null, out m_value);
            Text = m_value.ToString(m_format);            
            base.OnLostFocus(e);
        }



    //Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    //    If ((Me.Enabled) And (Not Me.ReadOnly)) Then
    //        Try

    //            Decimal.TryParse(Me.Text, System.Globalization.NumberStyles.Number, Nothing, m_value)
    //        Catch
    //        End Try
    //        Dim KeyAscii As Integer

    //        KeyAscii = AscW(e.KeyChar)

    //        If ((KeyAscii >= 48) And (KeyAscii <= 57) Or (KeyAscii = 8) Or (KeyAscii = 13)) Then  '0-9 backspace enter 
    //        ElseIf (KeyAscii = 45) Then '-
    //            If (Me.Text.IndexOf("-") >= 0) Then KeyAscii = 0
    //            If (Me.SelectionStart <> 0) Then KeyAscii = 0
    //        ElseIf (KeyAscii = 46) Then '.
    //            If (Me.Text.IndexOf(".") >= 0) Then KeyAscii = 0

    //        ElseIf (KeyAscii = 44) Then ',
    //            If (Me.Text.IndexOf(",") >= 0) Then KeyAscii = 0
    //        Else
    //            KeyAscii = 0
    //        End If
    //        e.Handled = (KeyAscii = 0)


    //    Else

    //        e.Handled = True
    //    End If
    //End Sub

    //Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
    //    If (Me.Text = "") Then Me.Text = "0"
    //    Decimal.TryParse(Me.Text, System.Globalization.NumberStyles.Number, Nothing, m_value)
    //    Me.Text = m_value.ToString(m_format)
    //    MyBase.OnLostFocus(e)
    //End Sub


    }
}
